<?php

namespace app\health\controller;

use app\health\model\HealthUserAddress;
use app\health\validate\UserAddressValidate;

class UserAddress extends ApiBase
{
    /**
     * 我的地址列表
     * @Author 张旭鹏
     * @DateTime 2023-06-14
     */
    public function lists()
    {
        $limit    = $this->request->param('limit/d', 10);
        $paginate = HealthUserAddress::where(['user_id' => $this->auth->id])
            ->field('id,username,mobile,province_name,city_name,county_name,is_default,address')
            ->order(['is_default' => 'desc', 'id' => 'desc'])
            ->paginate($limit);
        $this->success('', $paginate);
    }

    /**
     * 添加地址
     * @Author 张旭鹏
     * @DateTime 2023-06-14
     */
    public function store()
    {
        $username     = $this->request->param('username');
        $mobile       = $this->request->param('mobile');
        $province     = $this->request->param('province');
        $provinceName = $this->request->param('province_name', '');
        $city         = $this->request->param('city');
        $cityName     = $this->request->param('city_name', '');
        $county       = $this->request->param('county');
        $countyName   = $this->request->param('county_name', '');
        $address      = $this->request->param('address');
        $is_default   = $this->request->param('is_default');
        $id           = $this->request->param('id');
        if (!$username || !$mobile || !$province || !$city || !$county || !$address) {
            $this->error(__('参数错误'));
        }
        $userId = $this->auth->id;
        if ($is_default == 1) {
            //修改其它地址为非默认
            HealthUserAddress::where(['user_id' => $userId])->update(['is_default' => 0]);
        }
        $param['user_id']       = $userId;
        $param['username']      = $username;
        $param['mobile']        = $mobile;
        $param['province']      = $province;
        $param['province_name'] = $provinceName;
        $param['city']          = $city;
        $param['city_name']     = $cityName;
        $param['county']        = $county;
        $param['county_name']   = $countyName;
        $param['address']       = $address;
        $param['is_default']    = $is_default;
        $param['create_time']   = time();
        if ($id) {
            $address = HealthUserAddress::where(['id' => $id])->find();
            $address->save($param);
        } else {
            $address = HealthUserAddress::create($param);
        }
        if ($address) {
            $this->success(__('提交成功'), $address);
        }
        $this->error(__('提交失败'));
    }

    /**
     * 设置默认地址
     * @Author 张旭鹏
     * @DateTime 2023-06-14
     */
    public function setDefault()
    {
        $params = (new UserAddressValidate())->goCheck('set_default');
        //修改其它地址为非默认
        HealthUserAddress::where('user_id', $this->auth->id)->update(['is_default' => 0]);
        $res = HealthUserAddress::where('id', $params['id'])->update(['is_default' => 1]);
        if ($res) {
            $this->success(__('设置成功'));
        } else {
            $this->error(__('设置失败'));
        }
    }

    /**
     * 删除地址
     * @Author 张旭鹏
     * @DateTime 2023-06-14
     */
    public function delete()
    {
        $params = (new UserAddressValidate())->goCheck('set_default');
        //删除地址
        $res = HealthUserAddress::where(['id' => $params['id'], 'user_id' => $this->auth->id])->update(['delete_time' => time()]);
        if ($res) {
            $this->success(__('删除成功'));
        } else {
            $this->error(__('删除失败'));
        }
    }

    /**
     * 地址详情
     * @Author 张旭鹏
     * @DateTime 2023-06-14
     */
    public function detail()
    {
        $id   = $this->request->param('id');
        $data = HealthUserAddress::where('user_id', $this->auth->id)->where(['id' => $id])->find();
        $this->success(__('获取成功'), $data);
    }
}
